package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.CheckGroupService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //增加

    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
        } catch (Exception e) {
            return new Result(false,"新增角色失败");
        }
        return new Result(true,"新增角色成功");
    }

    //分页

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result = roleService.pageQuery(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return result;
    }

    @RequestMapping("/findPermission")
    public Result findPermission(){
            List<Permission> list = roleService.findPermission();
            if (list != null && list.size() > 0){
                return new Result(true,"查询成功",list);
            }
        return new Result(false,"查询失败");
    }

    @RequestMapping("/findfindMenu")
    public Result findfindMenu(){
        List<Menu> list = roleService.findMenu();
        if (list != null && list.size() > 0){
            return new Result(true,"查询成功",list);
        }
        return new Result(false,"查询失败");
    }

    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            Role byId = roleService.findById(id);
            return new Result(true,"查询成功",byId);
        } catch (Exception e) {
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/findByRPId")
    public Result findByRPId(Integer id){
        try {
            List<Integer> byRPId = roleService.findByRPId(id);
            return new Result(true,"查询成功",byRPId);
        } catch (Exception e) {
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/findByRMId")
    public Result findByRMId(Integer id){
        try {
            List<Integer> byRMId= roleService.findByRMId(id);
            return new Result(true,"查询成功",byRMId);
        } catch (Exception e) {
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            return new Result(false,"修改失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        int i = roleService.delete(id);
        if (i == 0) {
            return new Result(false, "删除失败,可能与user表存在关联");
        }
        return new Result(true, "删除成功");
    }


}
